package com.ae.shield.common.gui.container;

import com.ae.shield.common.block.BlockList;
import com.ae.shield.common.block.tile.ShieldWorkbenchTile;
import com.ae.shield.common.items.shieldFittings.makers.ShieldMakerBase;
import com.ae.shield.common.items.shieldFittings.SuffixItem;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.IWorldPosCallable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.items.SlotItemHandler;

import javax.annotation.Nonnull;

public class ShieldWorkbenchContainer extends Container {
    private final IWorldPosCallable callable;
    private final ShieldWorkbenchTile blockTile;

    public ShieldWorkbenchContainer(int id, PlayerInventory playerInventory, BlockPos pos) {
        super(ContainerList.SHIELD_WORKBENCH_CONTAINER.get(), id);
        World world = playerInventory.player.world;
        this.callable = IWorldPosCallable.of(world, pos);
        this.blockTile = (ShieldWorkbenchTile) world.getTileEntity(pos);
        loadSlot(playerInventory);
    }

    public ShieldWorkbenchContainer(int id, PlayerInventory playerInv, PacketBuffer buffer) {
        this(id, playerInv, buffer.readBlockPos());
    }

    private void loadSlot(PlayerInventory playerInv) {
        //创建玩家背包
        for (int row = 0; row < 3; row++) {
            for (int column = 0; column < 9; column++) {
                addSlot(new Slot(playerInv, column + row * 9 + 9, 8 + column * 18, 100 + row * 18));
            }
        }
        //创建工具栏
        for (int col = 0; col < 9; ++col) {
            addSlot(new Slot(playerInv, col, 8 + col * 18, 158));
        }
        //护盾格
        addSlot(new SlotItemHandler(blockTile.getInventory(), 0, 80, 65) {
            @Override
            public boolean isItemValid(@Nonnull ItemStack stack) {
                return blockTile.getInventory().isItemValid(0, stack);
            }
        });
        //后缀格
        for (int i = 0; i < blockTile.getInventory().getSlots() - 1; ++i) {
            int finalI = i;
            addSlot(new SlotItemHandler(blockTile.getInventory(), finalI + 1, 44 + 18 * finalI, 18) {
                @Override
                public boolean isItemValid(@Nonnull ItemStack stack) {
                    return blockTile.getInventory().isItemValid(finalI + 1, stack);
                }
            });
        }
    }

    @Nonnull
    @Override
    public ItemStack transferStackInSlot(@Nonnull PlayerEntity playerIn, int slotIn) {
        ItemStack stack;
        Slot slot = inventorySlots.get(slotIn);

        if (slot == null || !slot.getHasStack()) {
            return ItemStack.EMPTY;
        }

        ItemStack itemStack = slot.getStack();
        stack = itemStack.copy();
        int invStart = 0;
        int invEnd = invStart + 27;
        int shortEnd = invEnd + 9;
        int guiEnd = shortEnd + blockTile.getInventory().getSlots();

        if (slotIn < shortEnd) {
            if (itemStack.getItem() instanceof ShieldMakerBase) {
                if (mergeItemStack(itemStack, shortEnd, shortEnd + 1, false)) {
                    return ItemStack.EMPTY;
                }
            } else if (itemStack.getItem() instanceof SuffixItem) {
                if (mergeItemStack(itemStack, shortEnd + 1, guiEnd, false)) {
                    return ItemStack.EMPTY;
                }
            } else {
                return ItemStack.EMPTY;
            }
        } else {
            if (!mergeItemStack(itemStack, invEnd, shortEnd, false) && !mergeItemStack(itemStack, invStart, invEnd, false)) {
                return ItemStack.EMPTY;
            }
        }

        if (itemStack.isEmpty()) {
            slot.putStack(ItemStack.EMPTY);
        } else {
            slot.onSlotChanged();
        }

        if (itemStack.getCount() == stack.getCount()) {
            return ItemStack.EMPTY;
        }

        slot.onTake(playerIn, itemStack);

        return stack;
    }

    @Override
    public boolean canInteractWith(@Nonnull PlayerEntity playerIn) {
        return isWithinUsableDistance(callable, playerIn, BlockList.SHIELD_WORKBENCH_BLOCK.get());
    }

    public ITextComponent getDisplayName() {
        return blockTile.getDisplayName();
    }

    @OnlyIn(Dist.CLIENT)
    public float getEnergyScaled() {
        int i = blockTile.getEnergy().getMaxEnergyStored();
        int j = blockTile.getEnergy().getEnergyStored();
        return i == 0 || j == 0 ? 0 : (float) j / i;
    }

    public IFluidHandler getTank() {
        return blockTile.getTank();
    }

    public int getEnergy() {
        return blockTile.getEnergy().getEnergyStored();
    }

    @OnlyIn(Dist.CLIENT)
    public float getFluidScaled() {
        int i = blockTile.getTank().getTankCapacity(0);
        int j = blockTile.getTank().getFluidInTank(0).getAmount();
        return i == 0 || j == 0 ? 0 : (float)j / i;
    }

    public BlockPos getPos() {
        return blockTile.getPos();
    }
}
